package chapter2;

import java.util.Arrays;

public class QuickSort3 {
    public static void quickSort(int[] arr,int l,int r){
        if (l>=r){
            return;
        }
        int ret = (int)(l+Math.random()*(r-l+1));
        int num = arr[ret];
        int bound =l-1;
        swap(arr,ret,r);
        for (int i = l; i < r; i++) {
            if (arr[i] <= num){
                swap(arr,i,++bound);
            }
        }
        swap(arr,r,++bound);
        quickSort(arr,l,bound-1);
        quickSort(arr,bound+1,r);
    }

    public static void swap(int[] arr,int x,int y){
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
    public static void main(String[] args) {
        int[] arr ={4,2,5,6,8,3,7,9,9,2,1};
        quickSort(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}
